package com.ran.java_gobang_new.manager;

import org.springframework.context.annotation.Configuration;
import org.springframework.web.socket.WebSocketSession;

import java.util.concurrent.ConcurrentHashMap;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: ran
 * Date: 2025-09-03
 * Time: 21:05
 */
@Configuration
public class OnlineManager {
    // 存储用户id, 与 当前 websocket 会话, 主要用于记录游戏大厅的在线状态
    private ConcurrentHashMap<Integer, WebSocketSession> hall = new ConcurrentHashMap<>();

    // 存储用户 id 与 当前, 主要用于记录游戏房间的在线记录
    private ConcurrentHashMap<Integer, WebSocketSession> room = new ConcurrentHashMap<>();

    public void addRoom(Integer userId, WebSocketSession session) {
        room.put(userId,session);
    }

    public void removeRoom(Integer userId) {
        room.remove(userId);
    }

    public WebSocketSession getRoomSession(Integer userId) {
        return room.get(userId);
    }

    // 添加在线状态
    public void addHall(Integer userId, WebSocketSession session) {
        hall.put(userId,session); // 添加到在线状态
    }

    // 在线状态直接删除
    public void removeHall(Integer userId) {
        hall.remove(userId);
    }

    public WebSocketSession getHallSession(Integer userId) {
        return hall.get(userId);
    }
}
